/*
 * FileName: BaseDao.java
 *
 *  
 */
package com.mrc.core.base;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * Description:  基础Dao类
 * </p>
 *
 * @author Mrc
 * @version 4.1

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2016-12-22 上午10:19:28          Mrc        4.1         To create
 * </p>
 *
 * @since 
 * @see   
 * @param <E>  
 */
public abstract class BaseDao<E extends Serializable> {
    private Class<E> entityClass;

    /**
     * 自动注入sessionFactory
     */
    @Autowired
    private SessionFactory sessionFactory;

    protected BaseDao(Class<E> entityClass) {
        this.entityClass = entityClass;
    }

    /**
     * 获得Session
     * @return
     */
    public Session getCurrentSession() {
        return sessionFactory.getCurrentSession();
    }

    /**
     * 根据ID查找对象
     * @param id
     * @return
     */
    @SuppressWarnings("unchecked")
    public E findById(Serializable id) {
        return (E) getCurrentSession().get(entityClass, id);
    }

    /**
     * 保存对象
     * @param e
     */
    public void save(E e) {
        getCurrentSession().save(e);
    }

    /**
     * 删除对象
     * @param e
     */
    public void delete(E e) {
        getCurrentSession().delete(e);
    }

    /**
     * 查询HQL
     * @param hql
     * @param args
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<E> queryHQL(String hql, Object... args) {
        Query query = getCurrentSession().createQuery(hql);
        if (args != null) {
            for (int i = 0; i < args.length; i++) {
                query.setParameter(i, args[i]);
            }
        }
        return query.list();
    }

    /**
     * 分页查询数据
     * @param sql
     * @param pageNo
     * @param pageSize
     * @return
     */
    @SuppressWarnings("rawtypes")
    public PageDatas querySQL(String sql, Integer pageNo, Integer pageSize) {
        PageDatas pageDatas = new PageDatas();
        //当有页大小时候进行分页查询，查询总数，否则查询所有，节约资源
        if (pageSize != null && pageSize != 0) {
            //查询总数
            String countSql = "select count(*) from (" + sql + ") t ";
            Integer count = Integer.parseInt(getCurrentSession().createSQLQuery(countSql).list().get(0).toString());

            int start = (pageNo - 1) * pageSize;
            sql = sql + " limit " + start + "," + pageSize;
            //查询数据
            List list = getCurrentSession().createSQLQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
            pageDatas.setTotalLength(count);
            pageDatas.setDatas(list);
        } else {
            //查询数据
            List list = getCurrentSession().createSQLQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
            pageDatas.setTotalLength(-1);
            pageDatas.setDatas(list);
        }
        pageDatas.setPageNo(pageNo);
        pageDatas.setPageSize(pageSize);
        return pageDatas;
    }

}
